package cn.kgm.makeGod.action;

import cn.kgm.makeGod.Character;
import cn.kgm.makeGod.condition.EventResult;
import cn.kgm.makeGod.condition.WorldState;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @Version 1.8
 * @Author: Kappi
 * @Date: 2025-9-11 11:33
 * @Description: cn.kgm.makeGod.action.RollSuccessAction
 */
@Data
@Builder
@AllArgsConstructor
public class RollSuccessAction implements Action {

    private int baseSuccessRate;
    private String successBonusStat;
    private double successBonusScale;
    private List<Action> onSuccess = new ArrayList<>();
    private List<Action> onFailure = new ArrayList<>();

    public RollSuccessAction() {
    }

    public RollSuccessAction(int baseSuccessRate, String successBonusStat, double successBonusScale) {
        this.baseSuccessRate = baseSuccessRate;
        this.successBonusStat = successBonusStat;
        this.successBonusScale = successBonusScale;
    }

    @Override
    public void execute(Character actor, Character target, WorldState world, EventResult result) {
        // 计算最终成功率
        int statValue = 0;
        if ("charm".equals(successBonusStat)) statValue = actor.getCharm();
        else if ("luck".equals(successBonusStat)) statValue = actor.getLuck();

        int successRate = baseSuccessRate + (int) (statValue * successBonusScale);
        successRate = Math.max(0, Math.min(100, successRate));

        // 随机判定
        boolean success = new Random().nextInt(100) < successRate;
        result.addLog("成功率判定: " + successRate + "%, 结果: " + (success ? "成功" : "失败"));

        // 执行相应分支
        if (success) {
            for (Action action : onSuccess) {
                action.execute(actor, target, world, result);
            }
        } else {
            for (Action action : onFailure) {
                action.execute(actor, target, world, result);
            }
        }
    }
}
